package com.chenyue.cm.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.match.dao.PublicAreaMapper;
import com.chenyue.cm.match.domain.PublicArea;
import com.chenyue.cm.moudules.system.utils.ShiroUtils;
import com.chenyue.cm.user.service.JdFreightTemplateService;
import com.chenyue.cm.user.domain.JdFreightTemplate;
import com.chenyue.cm.user.domain.JdFreightTemplateExample;
import com.chenyue.cm.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
* @author chenyue-cm
* @date 2024-03-04
*/
@RestController
@RequestMapping("/jdFreightTemplate")
public class JdFreightTemplateController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JdFreightTemplateService JdFreightTemplateService;
    @Autowired
    private PublicAreaMapper publicAreaService;


    @RequestMapping("/getList")
    @ResponseBody
    public Success<PageVo<JdFreightTemplate>> getList(HttpServletRequest request, HttpServletResponse response) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdFreightTemplate> result = JdFreightTemplateService.getPageBean(pageInfo, JdFreightTemplate.class);
        return Success.success(result);
    }

    /**
     * 获取全国省名称以及编码
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getProvinceList")
    @ResponseBody
    public Success getProvinceList(HttpServletRequest request, HttpServletResponse response) {
        LambdaQueryWrapper<PublicArea> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PublicArea::getLevel, "p");
        List<PublicArea> publicAreas = publicAreaService.selectList(wrapper);
        return Success.success(publicAreas);
    }


    /**
     * @param vo
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    @Log
    public Success save(JdFreightTemplate vo, HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");
        if(vo.getType().equals("0")){
            LambdaQueryWrapper<JdFreightTemplate> wrapper = new LambdaQueryWrapper();
            wrapper.eq(JdFreightTemplate::getType,"0");
            wrapper.eq(JdFreightTemplate::getChargeType,vo.getChargeType());
            int selectCount = JdFreightTemplateService.count(wrapper);
            if(selectCount>0){
                return Success.error("通用模版已经存在,请更新");
            }
        }
        vo.setCreateTime(DateUtils.getCurrentTime());
        vo.setCreateUser(ShiroUtils.getUserId());
        JdFreightTemplateService.save(vo);
        return Success.success();
    }

    /**
     * @param vo
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    @Log
    public Success edit(JdFreightTemplate vo, HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");

        if (id != null && id != "") {
            if(vo.getType().equals("0")){
                JdFreightTemplate jdFreightTemplate = JdFreightTemplateService.getById(id);
                if(jdFreightTemplate.getType().equals("0")){
                    JdFreightTemplateService.getBaseMapper().updateById(vo);
                }else{
                    return Success.error("通用模版已经存在,请更新");
                }
            }else{
                JdFreightTemplateService.getBaseMapper().updateById(vo);
            }
        } else {
            return Success.error("id不能为空");
        }
        return Success.success();
    }


    /**
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/delete/{id}")
    @ResponseBody
    @Log
    public Success delete(@PathVariable String id, HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");

        JdFreightTemplateExample aExample = new JdFreightTemplateExample();
        aExample.createCriteria().andAddressEqualTo(id);
        JdFreightTemplateService.deleteByExample(aExample);

        return Success.success();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    @Log
    public Success delete(HttpServletRequest request, HttpServletResponse response, Model model) {
        response.setContentType("text/html;charset=utf-8");
        String ids = request.getParameter("ids");
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            JdFreightTemplateService.deleteByPrimaryKey(idString);
        }
        return Success.success();
    }
}
